﻿using System.Collections.ObjectModel;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using WhaleAccount.Contracts.Services;
using WhaleAccount.Models;
using WhaleAccount.ViewModels;
using Type = WhaleAccount.Models.Type;

namespace WhaleAccount.Views;

public sealed partial class ListDetailsDetailControl : UserControl
{
    private readonly IRecordService recordService;
    private readonly ITypeService typeService;
    private readonly MainViewModel mainViewModel;

    public DayRecordSummary? ListDetailsMenuItem
    {
        get => GetValue(ListDetailsMenuItemProperty) as DayRecordSummary;
        set => SetValue(ListDetailsMenuItemProperty, value);
    }

    public static readonly DependencyProperty ListDetailsMenuItemProperty =
        DependencyProperty.Register(
            "ListDetailsMenuItem",
            typeof(DayRecordSummary),
            typeof(ListDetailsDetailControl),
            new PropertyMetadata(null, OnListDetailsMenuItemPropertyChanged));
    public ObservableCollection<Type> TypeList
    {
        get; set;
    } = new ObservableCollection<Type>();

    public ListDetailsDetailControl()
    {
        recordService = App.GetService<IRecordService>();
        typeService = App.GetService<ITypeService>();
        mainViewModel = App.GetService<MainViewModel>();

        var types = typeService.GetTypeListAsync().Result;
        foreach (var type in types)
        {
            TypeList.Add(type);
        }

        InitializeComponent();
    }

    private static void OnListDetailsMenuItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is ListDetailsDetailControl control)
        {
            //control.ForegroundElement.ChangeView(0, 0, 1);
        }
    }

    private void RecordListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (DataContext is DayRecordSummary dayRecordSummary)
        {
            foreach (var item in e.AddedItems)
            {
                dayRecordSummary.SelectedRecords.Add(item as Record);
            }

            foreach (var item in e.RemovedItems)
            {
                dayRecordSummary.SelectedRecords.Remove(item as Record);
            }
        }
    }

    private void EditButton_Click(object sender, RoutedEventArgs e)
    {
        var editButton = sender as Button;
        if (editButton != null)
        {
            if (editButton.DataContext is Record record)
            {
                IdNumberBox.DataContext = record;
                if (record.Category == 0)
                {
                    ExpenseRadioButton.IsChecked = true;
                } else
                {
                    IncomeRadioButton.IsChecked = true;
                }
                TypeComboBox.SelectedValue = record.Type;
                DateCalendarDatePicker.Date= record.Date;
                ValueNumberBox.Value= record.Value;
                RemarkTextBox.Text= record.Remark;
            }
        }


        EditTeachingTip.Target = editButton;
        EditTeachingTip.IsOpen= true;
    }

    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        if (IdNumberBox.DataContext is Record record)
        {
            var valueChanged = record.Value != ValueNumberBox.Value;

            record.Category = ExpenseRadioButton.IsChecked.GetValueOrDefault(true) ? 0 : 1;
            record.Type = (int)TypeComboBox.SelectedValue;
            record.Value = ValueNumberBox.Value;
            record.Date = DateCalendarDatePicker.Date.GetValueOrDefault(DateTime.Now).DateTime;
            record.Remark = RemarkTextBox.Text;
            recordService.UpdateRecordAsync(record);

            if (valueChanged)
            {
                var saveButton = sender as Button;
                if (saveButton != null && saveButton.DataContext is DayRecordSummary dayRecordSummary)
                {
                    dayRecordSummary.Calculate();
                }

                mainViewModel.Calculate();
            }
        }

        EditTeachingTip.IsOpen = false;
    }
}
